<script setup lang="ts">
// import CpNavBar from '@/components/CpNavBar.vue'
import { login, loginByMobile, sendMobileCode } from '@/services/user'
import { useUserStore } from '@/stores'
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { showToast } from 'vant'
import { onUnmounted } from 'vue'
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
const route = useRoute()
const router = useRouter()
const store = useUserStore()
const mobile = ref('13230000001')
const password = ref('abc12345')
const agree = ref(false)
const onSubmit = async () => {
  if (!agree.value) return showToast('请勾选用户协议')
  // 登录合并
  let res = show.value
    ? await login(mobile.value, password.value)
    : await loginByMobile(mobile.value, code.value)
  store.setUser(res.data)
  router.push((route.query.returnUrl as string) || '/user')
  showToast('登录成功')
}
// ================================================================
// 切换短信验证码
const show = ref(true)
const time = ref(5)
const code = ref()
const isPassText = ref(true)
let timeId: number | undefined
const send = async () => {
  if (timeId) {
    return
  }
  // 发请求
  const res = await sendMobileCode(mobile.value, 'login')
  // 拿到结果做赋值操作
  code.value = res.data.code
  timeId = setInterval(() => {
    time.value--
    if (time.value == 0) {
      time.value = 5
      clearInterval(timeId)
      timeId = undefined
    }
  }, 1000)
}
// 图标单机切换文本框和密码框
const tabCode = () => {
  isPassText.value = !isPassText.value
}
const btn = () => {
  router.push('/register')
}
</script>

<template>
  <div class="login-page">
    <cp-nav-bar title="登录" right-text="注册" @click-right="btn"></cp-nav-bar>
    <!-- <cp-nav-bar title="登录" right-text="注册" ></cp-nav-bar> -->
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ show ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;" @click="show = !show">
        <span>{{ show ? '短信验证码登录' : '密码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" @submit="onSubmit">
      <van-field
        placeholder="请输入手机号"
        :rules="mobileRules"
        v-model="mobile"
        type="tel"
      ></van-field>
      <van-field
        placeholder="请输入密码"
        v-model="password"
        :rules="passwordRules"
        :type="isPassText ? 'password' : 'text'"
        v-if="show"
      >
        <template #button>
          <!-- <svg aria-hidden="true" @click="tabCode" class="cp-icon">
        
            <use
              :href="isPassText ? `#icon-login-eye-off` : `#icon-login-eye-on`"
            />
          </svg> -->
          <CpIcon
            @click="tabCode"
            :name="`login-eye-${isPassText ? 'off' : 'on'}`"
          ></CpIcon>
        </template>
      </van-field>

      <van-field
        v-else
        placeholder="短信验证码"
        :rules="codeRules"
        v-model="code"
      >
        <template #button>
          <span class="btn-send" @click="send" :class="{ active: time < 5 }">
            {{ time == 5 ? '发送验证码' : `${time}s后再次发送` }}
          </span>
        </template>
      </van-field>
      <!-- href='#icon'固定的写法  login-eye-off对应的文件夹的名字
        会把 icons里面的文件 当做href的路径展示
      -->

      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/qq.svg" alt="" />
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
.cp-icon {
  width: 1em;
  height: 1em;
}
</style>
